require("../util.js");
require("../component/components.js");
require("../component/seeHeader.js");
require("../component/footer.js");
require("../component/selfmediaflow.js");
require("../component/basicSlide.js");
require("../component/cityChoice.js");
require("../../jsMt/util.js");
var initData=JSON.parse(sessionStorage.getItem("data"));
var dataInfo=JSON.parse(sessionStorage.getItem("data"));
var userInfo=JSON.parse(sessionStorage.getItem("userInfo"));
if(!userInfo){
    location.href="/loginIndex"
}
var vm = new Vue({
    el:"#recruitPage",
    data:{
        loadFlag:false,
        categoryId:"",
        categoryId1:"",
        categoryName:"职位类别",
        indexs:1,
        categoryCarouselList:{},//顶部轮播
        // categorytopList:{},//top10列表
        pageNumber:"1",
        title:"",//标题
        areaId:"",//地点id
        areaName:"地点",
        salaryId:"",//薪水id
        salaryName:"薪水(元)",
        salaryList:[],
        recruitList:{},
        recruitList1:[],
        recruitList2:[],//职位列表
        // nearbyAdvList:{},
        // nearbyAdvList1:[],
        // nearbyAdvList2:[],//附近列表
        sortId:"user_count",
        circlesList:{},
        circlesList1:[],
        circlesList2:[],//圈子列表
        totalRow:"",//圈子总数
        isScreen:"",
        // chinaList:{
        //     provinces:[],//省
        //     cities:[],//市
        //     areas:[]//县
        // },//中国省市列表
        // provinceId:"",
        // cityId:"",
        // citiesList:[],//市列表
        // areasList:[],//区列表
        dataInfo:dataInfo,
        initData:initData,
        initId1:"",
        rCategorys:[],
        initId2:"",
        rCategorys1:[],
        message:"工作地点"
    },
    mounted:function(){
        this.searchObject = this.getQueryStringArgs();
        this.categoryId=this.searchObject&&this.searchObject.categoryId;
        // this.name=decodeURI(this.searchObject&&this.searchObject.name);
        // console.log(this.name);
        this.getCategoryCarouselList();
        this.getEnterpriseRecruitList();
        // this.getCategoryHotAdvList();
        this.getCategoryCirclesList();
        //this.getChinaList();
        lazyload();
    },
    methods:{
        //后退
        getBack:function(){
            history.go(-1);
        },
        bgimg:function(url){
            url = url?url:"";
            return {
                "background-image":'url("'+url+'"),url("")'
            }
        },
        /*获取查询字符对象*/
        getQueryStringArgs: function () {
            //取得查询字符串并去掉开头的问号
            var qs = (location.search.length > 0 ? location.search.substring(1) : ""),
                args = {},
                items = qs.length ? qs.split("&") : [],
                item = null,
                name = null,
                value = null,
                len = items.length;
            for (var i = 0; i < len; i++) {
                item = items[i].split("=");
                name = item[0];
                value = item[1];
                if (name.length) {
                    args[name] = value;
                }
            }
            return args;
        },
        //顶部轮播图
        getCategoryCarouselList:function(){
            var that=this;
            this.ajax({url:"/recruit/getCategoryCarouselList",data:{categoryId:that.categoryId},dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.categoryCarouselList=b.json.data;
                    //console.log(that.categoryCarouselList)
                }else{
                    vm.$refs.prompt.toast("获取轮播失败！")
                }
            });
        },
        //招聘主页职位列表
        getEnterpriseRecruitList:function(){
            var that=this;
            var data={
                categoryId:that.categoryId1,
                pageNumber:that.pageNumber,
                areaId:that.areaId,
                title:that.title,
                salaryId:that.salaryId,
                loginUid:userInfo.id
            };
            console.log(data)
            this.ajax({url:"/recruit/getEnterpriseRecruitList",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.recruitList=b.json.data;
                    that.recruitList1=that.recruitList.list;
                    if(that.recruitList.pageNumber>1){
                        if(that.recruitList.list.length>0) {
                            that.recruitList2 = that.recruitList2.concat(b.json.data.list);
                        }
                    }else{
                        that.recruitList2= that.recruitList1;
                    }
                    vm.loadFlag=false;
                    //console.log(that.categorytopList)
                }else{
                    vm.$refs.prompt.toast("获取职位列表失败！")
                }
            });
        },
        //获取分类页圈子列表
        getCategoryCirclesList:function(){
            var that=this;
            var data={
                categoryId:that.categoryId,
                pageNumber:that.pageNumber,
                sortId:that.sortId
            };
            this.ajax({url:"/recruit/getCategoryCirclesList",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.circlesList=b.json.data;
                    that.totalRow=that.circlesList.totalRow;
                    that.circlesList1=that.circlesList.list;
                    if(that.circlesList.pageNumber>1){
                        if(that.circlesList.list.length>0) {
                            that.circlesList2 = that.circlesList2.concat(b.json.data.list);
                            // that.circlesList2.push.apply(that.circlesList1,that.circlesList2);
                        }
                    }else{
                        that.circlesList2= that.circlesList1;
                    }
                    vm.loadFlag=false;
                    //console.log(that.totalRow)
                }else{
                    vm.loadFlag=false;
                    vm.$refs.prompt.toast("获取附近列表失败！")
                }
            });
        },
        //切换
        centTitleTab:function(index){
            this.indexs=index;
            this.pageNumber=1;
            if(this.indexs==1){
                this.getEnterpriseRecruitList();
            }else if(this.indexs==2){
                this.sortId="user_count";
                this.getCategoryCirclesList();
            }
        },
        //圈子筛选判断
        circleTab:function(num){
            if(num==0){
                this.sortId="user_count";
                this.circlesList={};
            }else if(num==1){
                this.circlesList={};
                this.sortId="create_date";
            }
            console.log(this.sortId);
            this.getCategoryCirclesList();
        },
        //下一页
        getNext:function(type,item){
            if(type==1){
                location.href="/see/recruitScreen?type=1&uid="+userInfo.id;
            }else if(type==2){
                location.href="/see/recruitVideo?uid="+userInfo.id+"&recruitId="+item;
            }else if(type==3){
                // location.href="/see/companyInfo?uid="+userInfo.id+"&enterpriseId="+item;
            }else if(type==4){
                var name=$(".seeInfo-navleft").find(".active").text();
                location.href="/friend/foundCircle?categoryId="+this.categoryId+"&name="+encodeURI(name);
            }
        },
        //个人中心
        getUser:function(type,item){
            console.log(item);
            if(type==1){
                location.href="/friend/circleBar?type=3&circleId="+item.circleId+"&uid="+userInfo.id;
            }else if(type==2){
                location.href="/userHomepage?id="+item.uid;
            }
        },
        getClass:function(type,item){
            console.log(item)
            var that=this;
            if(type==1){   //选择职位
                that.categoryId1=item.id;
                that.categoryName=item.name;
            }else if(type==2){
                that.salaryId=item.id;
                that.salaryName=item.value;
            }
            that.getEnterpriseRecruitList();
            that.getIsScreen();
        },
        choiceList:function(data){
            console.log(data)
            this.areaId=data.areasId;
            this.areaName=data.areasName;
            this.getEnterpriseRecruitList();
        },
        getIsScreen:function(num){
            var that=this;
            that.isScreen=num;
            if(num==1){
                that.rCategorys=[];
                that.rCategorys1=[];
                that.initId2="";
                that.initId1="";
            }else if(num==3){
                var arr={};
                that.salaryList=[];
                that.dataInfo.dict.forEach(function(val,key){
                    if(val.type=="hk_recruit_salary"){
                        arr={};
                        arr.id=val.id;
                        arr.value=val.label;
                        that.salaryList.push(arr);
                    }
                });
                console.log(that.salaryList)
            }
        },
        recruitCategorys:function(id,type){
            var that=this;
            if(type==1){
                that.rCategorys=[];
                that.rCategorys1=[];
                that.initId1=id;
            }else if(type==2){
                that.rCategorys1=[];
                that.initId2=id;
            }
            that.initData.recruitCategorys.forEach(function(item,index){
                if(item.parentId==id){
                    if(type==1){
                        that.rCategorys.push(item);
                    }else if(type==2){
                        that.rCategorys1.push(item);
                    }
                }
            });
            console.log(that.rCategorys);
        }
    }
    ,watch:{
        circlesList2:function(){
            if(this.circlesList2.length!=0){
                this.$nextTick(function(){
                    console.log($(".selfMedia-tabCent").height());
                    fmasonry();
                    $("img.lazy").lazyload({
                        failurelimit:10,
                        load:fmasonry
                    });
                });
            }
        },
        rCategorys:function(){
            this.$nextTick(function(){
                var that=this;
                var container = document.querySelectorAll('#screen .chooseCity-city');

                var expansion = null; //是否存在展开的list
                for(var i = 0; i < container.length; i++){
                    var x, y, X, Y, swipeX, swipeY;
                    container[i].addEventListener('touchstart', function(event) {
                        console.log(container)
                        x = event.changedTouches[0].pageX;
                        y = event.changedTouches[0].pageY;
                        swipeX = true;
                        swipeY = true ;
                        console.log("1111")
                        // if(expansion){   //判断是否展开，如果展开则收起
                        //     expansion.className ="chooseCity-city clearfix";
                        // }
                    });
                    container[i].addEventListener('touchmove', function(event){
                        event.stopPropagation();
                        X = event.changedTouches[0].pageX;
                        Y = event.changedTouches[0].pageY;
                        // 左右滑动
                        if(swipeX && Math.abs(X - x) - Math.abs(Y - y) > 0){
                            // 阻止事件冒泡
                            event.stopPropagation();
                            if(X - x > 10){   //右滑
                                // event.preventDefault();
                                this.className = "chooseCity-city clearfix";    //右滑收起
                            }
                            if(x - X > 10){   //左滑
                                event.preventDefault();
                                this.className = "chooseCity-city clearfix swipeleft";   //左滑展开
                                // expansion = this;
                            }
                            swipeY = false;
                        }
                        // 上下滑动
                        if(swipeY && Math.abs(X - x) - Math.abs(Y - y) < 0) {
                            swipeX = false;
                        }
                    });
                }
            });
        }
    }
});
// Vue.nextTick(function(){
//     fmasonry();
//     $("img.lazy").lazyload({
//         failurelimit:10,
//         load:fmasonry
//     });
// });
// $(function(){
//     fmasonry();
//     $("img.lazy").lazyload({
//         failurelimit:10,
//         load:fmasonry
//     });
// });

//文档高度
function getDocumentTop() {
    var scrollTop = 0, bodyScrollTop = 0, documentScrollTop = 0;
    if (document.body) {
        bodyScrollTop = document.body.scrollTop;
    }
    if (document.documentElement) {
        documentScrollTop = document.documentElement.scrollTop;
    }
    scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop;    return scrollTop;
}
//可视窗口高度
function getWindowHeight() {
    var windowHeight = 0;
    if (document.compatMode == "CSS1Compat") {
        windowHeight = document.documentElement.clientHeight;
    } else {
        windowHeight = document.body.clientHeight;
    }
    return windowHeight;
}
//滚动条滚动高度
function getScrollHeight() {
    var scrollHeight = 0, bodyScrollHeight = 0, documentScrollHeight = 0;
    if (document.body) {
        bodyScrollHeight = document.body.scrollHeight;
    }
    if (document.documentElement) {
        documentScrollHeight = document.documentElement.scrollHeight;
    }
    scrollHeight = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight;
    return scrollHeight;

}
window.onscroll=function(){
    // if(getDocumentTop()>41){
    //     vm.isNav=2;
    // }else{
    //     vm.isNav=1;
    // }
    var loadFlag;
    if(vm.indexs==1){
        loadFlag = vm.recruitList.lastPage;
    }else if(vm.indexs==2){
        loadFlag = vm.circlesList.lastPage;
    }
    //监听事件内容
    if(getScrollHeight()==getWindowHeight() + getDocumentTop()){
        //当滚动条到底时,这里是触发内容
        if(!loadFlag){
            vm.loadFlag=true;
            vm.pageNumber++;
            if(vm.indexs==1){
                vm.getEnterpriseRecruitList();
            }else if(vm.indexs==2){
                vm.getCategoryCirclesList();
            }
        }else{
            // console.log("11111");
            vm.$refs.prompt.toast("没有更多了！")
        }
    }
};
function fmasonry() {
    minigrid('.selfMedia-tabCent', '.selfMedia-list',9);
    // $(".selfMedia-tabCent").masonry({
    //     gutterWidth: 10,
    //     itemSelector: '.selfMedia-list',
    //     isAnimated: true,
    //     isFitWidth:true
    // });
}
function lazyload(){
    $("img.lazy").lazyload();
}